package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import org.springframework.stereotype.Service;

/**
 * @author TheCai
 * @date 2021/6/4 20:44
 */
@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry>
    implements ApBehaviorEntryService {

  /**
   * 根据登录用户id 或 设置id查询 出关联的用户实体
   *
   * @param userId
   * @param equipmentId
   * @return
   */
  @Override
  public ApBehaviorEntry findByUserIdOrEquipmentId(Integer userId, Integer equipmentId) {
    // 根据用户查询行为实体
    if (userId != null) {
      // 如果userid有查询行为实体
      return this.getOne(
          Wrappers.<ApBehaviorEntry>lambdaQuery()
              .eq(ApBehaviorEntry::getEntryId, userId)
              .eq(ApBehaviorEntry::getType, ApBehaviorEntry.Type.USER.getCode()));
    }

    if (equipmentId!=null){
      // 根据设备id查询行为实体
      return this.getOne(
          Wrappers.<ApBehaviorEntry>lambdaQuery()
              .eq(ApBehaviorEntry::getEntryId, equipmentId)
              .eq(ApBehaviorEntry::getType, ApBehaviorEntry.Type.EQUIPMENT.getCode()));
    }
    return null;
  }
}
